package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.domain.Category;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.CategoryBrandService;
import com.spzx.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * ClassName:CategoryBrandServiceImpl
 * Package:com.spzx.product.service.impl
 * Description:
 *
 * @Author:helei
 * @Create:2024/9/20 - 14:39
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand>
        implements CategoryBrandService {

    @Autowired
    private CategoryBrandMapper categoryBrandMapper;

    @Autowired
    private CategoryService categoryService;

    //查询分类品牌列表
    @Override
    public List<CategoryBrand> getPageList(CategoryBrand categoryBrand) {
        return categoryBrandMapper.selectCategoryBrandPageList(categoryBrand);
    }

    //获取分类品牌详细信息
    @Override
    public CategoryBrand selectCategoryBrandById(Long id) {
        CategoryBrand categoryBrand = this.getById(id);
        List<Long> categoryIdList = new ArrayList<>();
        Long categoryId = categoryBrand.getCategoryId();
        categoryIdList.add(categoryId);
        categoryIdList = getCategoryIdList(categoryIdList, categoryId);
//        Collections.sort(categoryIdList);
        categoryBrand.setCategoryIdList(categoryIdList);
        return categoryBrand;
    }

    //新增分类品牌
    @Override
    public boolean addCategoryBrand(CategoryBrand categoryBrand) {
        CategoryBrand one = this.getOne(Wrappers.lambdaQuery(CategoryBrand.class)
                .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId())
                .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
                .last("limit 1"));
        if (one == null) {
            categoryBrand.setDelFlag("0");
            return this.save(categoryBrand);
        }
        return false;
    }

    //修改分类品牌
    @Override
    public boolean updateCategoryBrand(CategoryBrand categoryBrand) {
        CategoryBrand one = this.getOne(Wrappers.lambdaQuery(CategoryBrand.class)
                .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId())
                .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
                .last("limit 1"));
        if (one == null || one.getId().equals(categoryBrand.getId())) {
            categoryBrand.setDelFlag("0");
            return this.updateById(categoryBrand);
        }
        return false;
    }

    //删除分类品牌
    @Override
    public boolean deleteCategoryBrand(Long[] ids) {

        return categoryBrandMapper.deleteCategoryBrand(ids);
    }

    public List<Long> getCategoryIdList(List<Long> categoryIdList, Long categoryId) {
//        int size = categoryIdList.size();
//        Long categoryId = categoryIdList.get(size - 1);
        Category category = categoryService.getById(categoryId);
        Long parentId = category.getParentId();
        if (parentId > 0) {
            categoryIdList.add(0, parentId);
            getCategoryIdList(categoryIdList, parentId);
        }
        return categoryIdList;
    }
}
